define(['app'], function(app) {
	'use strict';

	var controller = function controller($scope, Distributor, GoDialog, $ionicPopup, $timeout, GoPage, $window, $stateParams) {
		$scope.scope = new Object();
		$scope.input = new Object();
		$scope.input.count = 1;
		$scope.input.money = 1;
		$scope.input.memo = '';
		$scope.rule = 1;
		$scope.scope.reason = '';
		var store = new Array();
		$scope.isLoad = false;
		$scope.count = null;
		//画面加载
		$scope.$on('$ionicView.afterEnter', function() {
			var lan = 0; //这是状态栏，只有ios的APP才有
		var tab = 0;
		var bars = document.getElementById("nav-bar-title");
		var bar = ((bars != undefined) ? 44 : 0);
		if(window.cordova && window.cordova.plugins && ionic.Platform.isIOS()) { lan = 20; }
		//整体高度
		var all = $window.innerHeight;
		var distributor_top1 = document.getElementById("distributor-top1").clientHeight;
		$scope.distributor_myObj = { //滚动条可视范围
			"height": all - lan - bar - tab - distributor_top1 + "px"
		};
		$scope.is_init = $stateParams.is_init;
			if($scope.is_init == true) {
				$scope.init_data();
			}
		}, false);
		//顶部的跳转
		$scope.onshow_menu = function(index, store_id) {
			var url = "";
			if(index == 1) {
				url = "distributor_add";
				GoPage.go_states(url, { "store_id": store_id });
			}
			if(index == 2) {
				url = "distributor_list";
				GoPage.go_states(url);
			}
		}
		$scope.go_store = function(store_id) {
			GoPage.go_states('first', { 'store_id': store_id });
		}
		//按钮的跳转
		$scope.onshow_button = function(index, store_id) {
			var url = "";
			if(index == 1) url = "distributor_order";
			if(index == 2) url = "distributor_creditrepayview";

			GoPage.go_states(url, {
				id: store_id
			});
		}

		var myPopup_red;
		$scope.showPopup_red = function(val) { //绑定数据时要注意，如果是数组，则通过$index传入下标；如果是json，则传入key
			$scope.input.memo = '';
			$scope.input.money = 1;
			myPopup_red = $ionicPopup.show({ //show()
				templateUrl: 'templates/modal/give_redbag.html',
				title: "发红包",
				scope: $scope, //绑定数据，必须写这行
				buttons: [{
					text: "确定",
					type: "button-assertive",
					onTap: function(e) { // 当点击保存按钮时，执行该函数
						//console.log($scope.scope.reason);
						//return $scope.scope.reason;//
						if($scope.input.money == '') {
							GoDialog.toast("请输入红包金额");
							e.preventDefault();
							return;
						} else {
							//			

							store = new Array;
							var n = store.length;
							store[n] = {
								"store_id": val
							};
							Distributor.packets_grant(1, $scope.input.money, $scope.input.memo, 1, store, function(data) {
								if(data.success == true) {
									$scope.input.advance = data.result.member_advance;
									GoDialog.toast("发送成功");
								} else {
									GoDialog.toast(data.msg);
									e.preventDefault();
								}

							})
						}
					}
				}]
			});
		};
		//点击其他区域关闭弹出窗       
		var htmlEl = angular.element(document.querySelector('html'));
		htmlEl.on('click', function(event) {　　
			if(event.target.nodeName === 'HTML') {　　　　
				if(myPopup_red) {　　　　　　
					myPopup_red.close();　　　　
				}　　　　
			}
		});
		$scope.credit_change = function(store_id, price) {
			Distributor.credit_change(store_id, price, function(data) {
				if(data.success == true) {
					GoDialog.toast('授信额度修改成功！');
					$scope.init_data();
					return;
				} else
					GoDialog.toast(data.msg);
			})
		}
		$scope.price_change = function(store_id, brand_id, is_change) {
			Distributor.price_change(store_id, brand_id, !is_change, function(data) {
				if(data.success == true) {
					if(!is_change == true) {
						GoDialog.toast('授信修改设置成功')
					} else {
						GoDialog.toast('取消授信设置成功')
					}
					$scope.init_data();
				} else
					GoDialog.toast(data.msg);
			})
		}
		$scope.init_data = function() {
			Distributor.my_distributor(1, function(data) {
				if(data != null) {
					$scope.datas = data;
					$scope.input.advance = data.member_advance;
				} else
					$scope.datas = new Object();
				$scope.input.nPage = 1;

			})
		}
		//删除按钮
		$scope.delete_button = function(store_id) {
			Distributor.delete_agent(store_id, function(data) {
				if(data.success == true) {
					GoDialog.toast("刪除成功");
					$scope.init_data();
				} else
					GoDialog.toast(data.msg);

			});

		}
		$scope.packets = function(store_id) {
			Distributor.packets_grant('count', 'money', 'memo', 'store_id', function(data) {
				if(data.success == true) {
					GoDialog.toast("发送成功");
				} else
					GoDialog.toast("发送失败");
			})
		}
		$scope.doRefresh = function() {
			$scope.isLoad = true;
			Distributor.my_distributor(1, function(data) {
//				console.log(data);
				if(data != null)
					$scope.datas = data;
				else
					$scope.datas = new Object();
				$scope.input.nPage = 1;
			}, function() {
				$scope.$broadcast('scroll.refreshComplete');
				$timeout(function() {
					$scope.isLoad = false;
				}, 1500);
			})
		}
		$scope.doRefreshMore = function() {
			$scope.isLoad = true;

			if($scope.datas.store == undefined) 
				$scope.input.nPage = 0;
			else if($scope.datas.page.count <= $scope.datas.store.length) {
					$scope.$broadcast('scroll.infiniteScrollComplete');
					if($scope.datas.page.count<$scope.input.nPage*10){
					$scope.input.nPage -=1;
					$scope.count = $scope.input.nPage *10;
				}else{
					$scope.count = null;
				}
				}

			Distributor.my_distributor($scope.input.nPage + 1, function(data) {
//				console.log(data);
				if(data != null) {
					if($scope.datas.store != undefined) {
						if($scope.count!=null)
						$scope.datas.store.splice($scope.count,$scope.datas.page.count-$scope.count);
						$scope.datas.store = $scope.datas.store.concat(data.store);
						$scope.input.nPage += 1;
					} else {
						$scope.datas = data;
						$scope.input.nPage = 1;
					}
				}

			}, function() {
				$scope.$broadcast('scroll.infiniteScrollComplete');
				$timeout(function() {
					$scope.isLoad = false;
				}, 1500);
			})
		}
		$scope.init_data();
	}

	controller.$inject = ['$scope', 'Distributor', 'GoDialog', '$ionicPopup', '$timeout', 'GoPage', '$window', '$stateParams'];

	return app.registerController("distributorctrl",controller);
});